// Copyright (c) 2009-2010, Jeremy Brewer
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package galaxie500.benchmarks;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.Random;

import galaxie500.io.ColumnReader;
import galaxie500.util.Timer;

/**
 * Simple benchmark for file reading code. No file is used; instead a large file
 * is created in memory and then read.
 * 
 * @author Jeremy Brewer
 */
public class ColumnReaderBenchmark {

  /**
   * @param args Not used.
   */
  public static void main(String[] args) {
    Timer timer = new Timer();
    timer.start();

    // TODO: write a large temporary file to disk instead
    System.out.printf("Building file in memory... ");
    StringBuilder builder = new StringBuilder();
    int numLines = 10000;
    Random random = new Random();

    for (int i = 0; i < numLines; i++) {
      builder.append(random.nextDouble());
      builder.append("  ");
      builder.append(random.nextDouble());
      builder.append("\n");
    }

    String data = builder.toString();
    timer.stop();
    System.out.printf("done in %.3f sec\n", timer.getTimeSec());

    timer.reset();
    timer.start();
    System.out.printf("Reading file... ");

    ColumnReader reader =
        new ColumnReader(new BufferedReader(new StringReader(data)), 2);

    try {
      while (reader.readLine()) {
        String[] columns = reader.getColumns();
        assert reader.getNumColumns() == 2 : "Didn't read two columns";
        double a = Double.parseDouble(columns[0]);
        double b = Double.parseDouble(columns[1]);
        assert !Double.isInfinite(a) && !Double.isNaN(a) : "Bad input: " + a;
        assert !Double.isInfinite(b) && !Double.isNaN(b) : "Bad input: " + b;
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      reader.close();
    }

    timer.stop();
    System.out.printf("done in %.3f sec\n", timer.getTimeSec());
    System.out.printf("Read %d lines (%g lines per sec)\n", numLines,
                      (double) numLines / timer.getTimeSec());
  }
}